﻿using System;
using System.Runtime.CompilerServices;

namespace Common.Msg
{
    /// <summary>
    /// 记录系统日志公用类
    /// 默认只开启错误
    /// 开启Info日志记录请配置InfoLog为true
    /// 开启Warn日志记录请配置WarnLog为true
    /// </summary>
    public class log4netLogger
    {
        private static log4net.ILog log;
        static log4netLogger()
        {
            string baseDir = System.AppDomain.CurrentDomain.BaseDirectory;
            if (!baseDir.EndsWith("\\"))
            {
                baseDir += "\\";
            }
            System.IO.FileInfo file = new System.IO.FileInfo(baseDir + "log4net.config");
            log4net.Config.XmlConfigurator.Configure(file);
            log = log4net.LogManager.GetLogger("myName");
        }


        /// <summary>
        /// 记录系统异常日志
        /// </summary>
        /// <param name="methodName">异常来源方法名称</param>
        /// <param name="customMsg">自定义错误消息</param>
        /// <param name="ex">系统异常信息</param>
        public static void Error(string methodName, string customMsg, System.Exception ex = null)
        {
            log.Error("错源方法：" + methodName + "\r\n错误提示：" + customMsg, ex);
        }
        /// <summary>
        /// 记录系统异常日志
        /// </summary>
        /// <param name="methodName">异常来源方法名称</param>
        /// <param name="customMsg">自定义错误消息</param>
        /// <param name="controllerName">操作方法控制器名称</param>
        /// <param name="actionName">操作方法名称</param>
        /// <param name="dllName">程序集名称</param>
        /// <param name="ex">系统异常信息</param>
        public static void Error(string methodName, string customMsg, string controllerName, string actionName, string dllName = "", System.Exception ex = null)
        {
            log.Error(string.Format("操作方法：{0}.{1}\r\n错源程序集：{2}\r\n错源方法：{3}\r\n错误提示：{4}", controllerName, actionName, dllName, methodName, customMsg), ex);
        }
        public static void Error(string customMsg, System.Exception ex = null, System.Type type = null, [CallerMemberName] string methodName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            log.Error(string.Format("错源方法：{0}\r\n所在文件：{1}\r\n所在行号：{2}\r\n错误提示：{3}", (type != null ? type.FullName + "." : "") + methodName, filePath, lineNumber, customMsg), ex);
        }


        /// <summary>
        /// 记录系统信息
        /// </summary>
        /// <param name="content">信息</param>
        /// <param name="methodName">异常来源方法名称</param>
        /// <param name="filePath">调用者源文件的完整路径</param>
        /// <param name="lineNumber">源文件的行号</param>
        public static void Info(string content, System.Type type = null, [CallerMemberName] string methodName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            log.Info(string.Format("输出方法：{0}\r\n操作时间：{1}\r\n所在文件：{2}\r\n所在行号：{3}\r\n输出信息：{4}", (type != null ? type.FullName + "." : "") + methodName, System.DateTime.Now, filePath, lineNumber, content));
        }


        /// <summary>
        /// 记录系统警告信息
        /// </summary>
        /// <param name="content">信息</param>
        /// <param name="methodName">异常来源方法名称</param>
        /// <param name="filePath">调用者源文件的完整路径</param>
        /// <param name="lineNumber">源文件的行号</param>
        public static void Warn(string content, System.Type type = null, [CallerMemberName] string methodName = "", [CallerFilePath] string filePath = "", [CallerLineNumber] int lineNumber = 0)
        {
            log.Warn(string.Format("警告方法：{0}\r\n操作时间：{1}所在文件：{2}\r\n所在行号：{3}\r\n警告信息：{4}", (type != null ? type.FullName + "." : "") + methodName, System.DateTime.Now, filePath, lineNumber, content));
        }
    }
}
